Test Failed
Pull Request — master (#2)
by Luís
03:32 queued 01:49
created

???.fromBeutyToSeconds   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
c 0
b 0
f 0
nc 16
dl 0
loc 29
rs 8.439
nop 1
1
export default {
2
    /**
3
     * @param {String|Integer} num add a leading 0 to help format dates and times
4
     * @returns {String}
5
     */
6
    leadingZero: function (num) {
7
        return `0${num}`.slice(-2);
8
    },
9
10
    /**
11
     * Receive a number of seconds and return an string representing the amount of
12
     * hours on the format: 00h00m00s
13
     * Examples:
14
     * beautifySeconds(60, false) => 01m
15
     * beautifyMinutes(120, false) => 02m
16
     * beautifyMinutes(3900, false) => 1h05m
17
     * beautifyMinutes(3900, true) => 1h05m00s
18
     * @param {Number} seconds
19
     * @param {Boolean} showSeconds
20
     * @returns {string}
21
     */
22
    beautifySeconds: function (seconds, showSeconds = true) {
23
        let response = "";
24
        let theTime = {
25
            hours: 0,
26
            minutes: 0,
27
            seconds: 0
28
        };
29
30
        theTime.hours = ~~(seconds / 3600);
31
        theTime.minutes = ~~((seconds % 3600) / 60);
32
        theTime.seconds = seconds % 60;
33
34
        if (theTime.hours) {
35
            response += `${theTime.hours}h`;
36
            response += `${this.leadingZero(theTime.minutes)}m`;
37
            response += showSeconds ? `${this.leadingZero(theTime.seconds)}s` : "";
38
        } else if (theTime.minutes) {
39
            response += `${this.leadingZero(theTime.minutes)}m`;
40
            response += showSeconds ? `${this.leadingZero(theTime.seconds)}s` : "";
41
        } else if (showSeconds) {
42
            response += `${this.leadingZero(theTime.seconds)}s`;
43
        }
44
45
        return response;
46
    },
47
48
    /**
49
     * @see beautifySeconds
50
     * Works the same way as beautifySeconds, but receive an amount of minutes
51
     * @param {Number} minutes
52
     * @param {Boolean} showSeconds
53
     * @returns {String}
54
     */
55
    beautifyMinutes: function (minutes, showSeconds = true) {
56
        return this.beautifySeconds(minutes * 60, showSeconds);
57
    },
58
59
    
60
    /**
61
     * Receives an duration with the format 00h00m00s and returns the amount 
62
     * of seconds. The inverse of beautifySeconds
63
     * 
64
     * @param {String} theTime
65
     * @returns {Number}
66
     */
67
    fromBeutyToSeconds: function (theTime) {
68
        let response = 0;
69
70
        const hRE = /(\d+)h/;
71
        const mRE = /(\d+)m/;
72
        const sRE = /(\d+)s/;
73
74
        let hours = hRE.exec(theTime);
75
        let minutes = mRE.exec(theTime);
76
        let seconds = sRE.exec(theTime);
77
78
        if (hours) {
79
            response += parseInt(hours[1], 10) * 3600;
80
        }
81
82
        if (minutes) {
83
            response += parseInt(minutes[1], 10) * 60;
84
        }
85
86
        if (seconds) {
87
            response += parseInt(seconds[1], 10);
88
        }
89
90
        if (!response) {
91
            response += parseInt(theTime, 10);
92
        }
93
94
        return response;
95
    },
96
97
    
98
    /**
99
     * Receive two dates and returns the amount of days between them
100
     * Dates on the format supported by the Date constructor
101
     * 
102
     * @param {String} startDateString
103
     * @param {String} endDateString
104
     * @returns {Number}
105
     */
106
    daysBetween: function (startDateString, endDateString) {
107
        const oneDay = 24 * 60 * 60 * 1000;
108
        const startDate = new Date(startDateString);
109
        const endDate = new Date(endDateString);
110
111
        return Math.round(
112
            Math.abs((startDate.getTime() - endDate.getTime()) / oneDay)
113
        );
114
    },
115
116
    /**
117
     * Returns the current date on the format: yyyy-mm-dd
118
     * @returns {String}
119
     */
120
    curdate: () => new Date().toISOString().substr(0, 10),
121
122
    /**
123
     * Returns the first day of the current month on the format: yyyy-dd-mm
124
     * @returns {String}
125
     */
126
    firstDayOfTheMonth: function () {
127
        const today = new Date();
128
        return `${today.getFullYear()}-${today.getMonth() + 1}-01`;
129
    },
130
131
    /**
132
     * Returns the last day of the month on the format: yyyy-mm-dd
133
     * It accepts an optional month and year to get the last day of an particular month
134
     * @param {String} month
135
     * @param {String} year
136
     * @returns {String}
137
     */
138
    lastDayOfTheMonth: function (month, year) {
139
        const today = new Date();
140
        const useMonth = month ? month : today.getMonth() + 1;
141
        const useYear = year ? year : today.getFullYear();
142
        const lastDay = new Date(useYear, useMonth, 0);
143
144
        return `${useYear}-${this.leadingZero(useMonth)}-${this.leadingZero(lastDay.getDate())}`;
145
    }
146
};
147